/**
 * 通用列表
 * hanson
 */
define(['jquery', 'layer'], function($, Layer){
	
	var DOM = {
			formList: $('#formList'),
			pageNumber: $('#formList input[name=pageNumber]'),
			pageSize: $('#formList input[name=pageSize]'),
			searchProperty: $('#formList input[name=searchProperty]'),
			searchValue: $('#formList input[name=searchValue]'),
			
			tableList: $(".tableList"),
			selectAll: $('#selectAll'),
			btnDelete: $('.divAction .btnDelete'),
			btnRefresh: $(".divAction .btnRefresh"),
			ids: $(".tableList input[name='ids']"),
			contentRow: $(".tableList tr:gt(0)"),
			searchBox: $('#searchBox'),
			spanSearchProperty: $('#searchBox .spanSearchProperty')
	}
	
	// 搜索下拉框
	DOM.searchBox.find('.dropdownMenu li a').click(function(){
		var $this = $(this);
		var searchProperty = $this.data('search-property');
		DOM.spanSearchProperty.text($this.text()).data('search-property', searchProperty);
	});
	
	// 加载下拉搜索框
	if(DOM.spanSearchProperty.data('search-property') != ''){
		var current = DOM.spanSearchProperty.data('search-property');
		DOM.searchBox.find('.dropdownMenu li a').each(function(){
			if($(this).data('search-property') == current){
				DOM.spanSearchProperty.text($(this).text());
			}
		});
	}
	
	// 全选
	DOM.selectAll.click( function() {
		var $this = $(this);
		var $enabledIds = DOM.tableList.find("input[name='ids']:enabled");
		if ($this.prop("checked")) {
			$enabledIds.prop("checked", true);
			if ($enabledIds.filter(":checked").length > 0) {
				DOM.btnDelete.removeClass("disabled");
				DOM.contentRow.addClass("selected");
			} else {
				DOM.btnDelete.addClass("disabled");
			}
		} else {
			$enabledIds.prop("checked", false);
			DOM.btnDelete.addClass("disabled");
			DOM.contentRow.removeClass("selected");
		}
	});
	
	// 选择
	DOM.ids.click( function() {
		var $this = $(this);
		if ($this.prop("checked")) {
			$this.closest("tr").addClass("selected");
			DOM.btnDelete.removeClass("disabled");
		} else {
			$this.closest("tr").removeClass("selected");
			if (DOM.tableList.find("input[name='ids']:enabled:checked").length > 0) {
				DOM.btnDelete.removeClass("disabled");
			} else {
				DOM.btnDelete.addClass("disabled");
			}
		}
	});
	
	// 刷新
	DOM.btnRefresh.click( function() {
		location.reload(true);
		return false;
	});
	
	// 删除
	DOM.btnDelete.click( function() {
		var $this = $(this);
		if ($this.hasClass("disabled")) {
			return false;
		}
		var $checkedIds = DOM.tableList.find("input[name='ids']:enabled:checked");
		Layer.confirm('确认删除？',function(){
			$.ajax({
				url: "delete",
				type: "POST",
				data: $checkedIds.serialize(),
				dataType: "json",
				cache: false,
				success: function(message) {
					if (message.type == "success") {
						Layer.msg('操作成功');
						$checkedIds.closest("tr").remove();
						if (DOM.tableList.find("tr").length <= 1) {
							setTimeout(function() {
								location.reload(true);
							}, 3000);
						}
					}
					DOM.btnDelete.addClass("disabled");
					DOM.selectAll.prop("checked", false);
					$checkedIds.prop("checked", false);
				}
			});
		});
		return false;
	});
	
	// 表单提交
	DOM.formList.submit(function() {
		if (!/^\d*[1-9]\d*$/.test(DOM.pageNumber.val())) {
			DOM.pageNumber.val("1");
		}
		if (DOM.searchValue.length > 0 && DOM.searchValue.val() != "") {
			DOM.searchProperty.val(DOM.spanSearchProperty.data('search-property'));
		}
	});
	
});